<?php
require_once 'service.php';
require_once 'util/constants.php';
require_once('model/SurveysManager.php');
require_once('model/UserManager.php');
require_once('exceptions/SurveysException.php');

class SurveysService extends Service {
	
	/* models */
	protected $oSurveysManager;	// connection to surveys collection
	protected $oUserManager;		// connection to users collection
	
    public function __construct() {
        $this->oSurveysManager = new SurveysManager();
        $this->oUserManager = new UserManager();
                        
        parent::__construct('html/surveys.html', true);        
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function getSurveys() {
        try {
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $asStati = json_decode($this->asParams['asStati']);
            $this->addToResponse($this->oSurveysManager->getSurveysAsArrays($oUser, $asStati), "aoSurveys");
            $this->success();
        } catch(Exception $e) {
            $this->error($e->getMessage());
        }
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function getSurvey() {
        try {
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $this->addToResponse($this->oSurveysManager->getSurveyAsArray($oUser, $this->asParams['id']), "oSurvey");
            $this->success();
        } catch(Exception $se) {
            $this->error($se->getMessage());
        }
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function getNewSurvey() {
        try {
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $this->addToResponse($this->oSurveysManager->getNewSurveyAsArray($oUser), "oSurvey");
            $this->success();
        } catch(Exception $se) {
            $this->error($se->getMessage());
        }
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function getResult() {
        try {
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $this->addToResponse($this->oSurveysManager->getResult($this->asParams['id']), "oSurvey");
            $this->success();
        } catch(Exception $e) {
            $this->error($e->getMessage());
        }
    }
    
    /**
     * 
     * Enter description here ...
     */
    public function save() {        
        try {
            $oUser = $this->oUserManager->getUser($this->sUserID, $this->sHash);
            $this->addToResponse($this->oSurveysManager->saveSurvey($oUser, $this->asParams), "oSurvey");
            $this->success();
        } catch(Exception $e) {
            $this->error($e->getMessage());
        }
    }
}

$SurveysService = new SurveysService();
?>
